/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.core;

import java.awt.Color;
import java.util.logging.Logger;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.util.logging.Level;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import org.netbeans.modules.tfsnb.core.ConnectionSettings.PROTOCOL;

/**
 *
 * @author Venkat Ram Akkineni
 */
public class AddTFSConnectionPanel extends javax.swing.JPanel {

    private static final AddTFSConnectionPanelController controller;    
    private static final Logger LOG = Logger.getLogger(AddTFSConnectionPanel.class.getName());
    
    private ConnectionSettings cs;
    
    static {
        controller = new AddTFSConnectionPanelController();
    }
    
    
    /**
     * Creates new form AddTFSConnectionPanel
     */
    public AddTFSConnectionPanel(boolean toggleCredentialsFields) {
        initComponents();
        toggleCredentialsFields(toggleCredentialsFields);
    }
    
    /**
     * Creates new form AddTFSConnectionPanel
     */
    public AddTFSConnectionPanel(final ConnectionSettings cs) {
        this(true);        
        nameTextField.setText(cs.getName());
        pathTextField.setText(cs.getPath());
        portTextField.setText(cs.getPortNumber());
        serverUrlTextField.setText(cs.getServerUrl());
        saveCredentialsCheckBox.setSelected(cs.isSaveCredentials());
        
        if(cs.getProtocol().equals(PROTOCOL.HTTP)) {
            httpRadioButton.setSelected(true);
        } else {
            httpsRadioButton.setSelected(true);
        }
        
        if(cs.isSaveCredentials()) {
            usernameTextField.setText(cs.getUsername());
            passwordField.setText(new String(cs.getPassword()));            
        }
        
        this.cs = cs;
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        protocolButtonGroup = new javax.swing.ButtonGroup();
        serverUrlLabel = new javax.swing.JLabel();
        serverUrlTextField = new javax.swing.JTextField();
        connectionDetailsPanel = new javax.swing.JPanel();
        pathLabel = new javax.swing.JLabel();
        pathTextField = new javax.swing.JTextField();
        portTextField = new javax.swing.JTextField();
        portLabel = new javax.swing.JLabel();
        protocolLabel = new javax.swing.JLabel();
        httpRadioButton = new javax.swing.JRadioButton();
        httpsRadioButton = new javax.swing.JRadioButton();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        testButton = new javax.swing.JButton();
        previewLabel = new javax.swing.JLabel();
        previewTextField = new javax.swing.JTextField();
        testConnectionLabel = new javax.swing.JLabel();
        credentialsPanel = new javax.swing.JPanel();
        passwordField = new javax.swing.JPasswordField();
        passwordLabel = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        saveCredentialsCheckBox = new javax.swing.JCheckBox();
        usernameFormatLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(serverUrlLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.serverUrlLabel.text")); // NOI18N

        serverUrlTextField.getDocument().addDocumentListener(documentListener);

        connectionDetailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.connectionDetailsPanel.border.title"), javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION)); // NOI18N

        pathLabel.setLabelFor(pathTextField);
        org.openide.awt.Mnemonics.setLocalizedText(pathLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.pathLabel.text")); // NOI18N

        pathTextField.getDocument().addDocumentListener(documentListener);

        portTextField.getDocument().addDocumentListener(documentListener);

        portLabel.setLabelFor(portTextField);
        org.openide.awt.Mnemonics.setLocalizedText(portLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.portLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(protocolLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.protocolLabel.text")); // NOI18N

        protocolButtonGroup.add(httpRadioButton);
        httpRadioButton.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(httpRadioButton, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.httpRadioButton.text")); // NOI18N
        httpRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                httpRadioButtonActionPerformed(evt);
            }
        });

        protocolButtonGroup.add(httpsRadioButton);
        org.openide.awt.Mnemonics.setLocalizedText(httpsRadioButton, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.httpsRadioButton.text")); // NOI18N
        httpsRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                httpsRadioButtonActionPerformed(evt);
            }
        });

        nameLabel.setLabelFor(pathTextField);
        org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.nameLabel.text")); // NOI18N

        nameTextField.getDocument().addDocumentListener(documentListener);

        javax.swing.GroupLayout connectionDetailsPanelLayout = new javax.swing.GroupLayout(connectionDetailsPanel);
        connectionDetailsPanel.setLayout(connectionDetailsPanelLayout);
        connectionDetailsPanelLayout.setHorizontalGroup(
            connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                        .addComponent(portLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(portTextField))
                    .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                        .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nameTextField))
                    .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                        .addComponent(protocolLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(httpRadioButton)
                        .addGap(18, 18, 18)
                        .addComponent(httpsRadioButton)
                        .addGap(0, 141, Short.MAX_VALUE))
                    .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                        .addComponent(pathLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pathTextField)))
                .addContainerGap())
        );
        connectionDetailsPanelLayout.setVerticalGroup(
            connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pathLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(portLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(protocolLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(connectionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(httpRadioButton)
                        .addComponent(httpsRadioButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.openide.awt.Mnemonics.setLocalizedText(testButton, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.testButton.text")); // NOI18N
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(previewLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.previewLabel.text")); // NOI18N

        previewTextField.setEnabled(false);
        nameTextField.getDocument().addDocumentListener(documentListener);

        testConnectionLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.openide.awt.Mnemonics.setLocalizedText(testConnectionLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.testConnectionLabel.text")); // NOI18N

        credentialsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.credentialsPanel.border.title"), javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION)); // NOI18N

        passwordField.setText(org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.passwordField.text")); // NOI18N
        passwordField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        passwordField.getDocument().addDocumentListener(documentListener);

        passwordLabel.setLabelFor(portTextField);
        org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.passwordLabel.text")); // NOI18N

        usernameTextField.getDocument().addDocumentListener(documentListener);

        usernameLabel.setLabelFor(portTextField);
        org.openide.awt.Mnemonics.setLocalizedText(usernameLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.usernameLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(saveCredentialsCheckBox, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.saveCredentialsCheckBox.text")); // NOI18N
        saveCredentialsCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCredentialsCheckBoxActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(usernameFormatLabel, org.openide.util.NbBundle.getMessage(AddTFSConnectionPanel.class, "AddTFSConnectionPanel.usernameFormatLabel.text")); // NOI18N

        javax.swing.GroupLayout credentialsPanelLayout = new javax.swing.GroupLayout(credentialsPanel);
        credentialsPanel.setLayout(credentialsPanelLayout);
        credentialsPanelLayout.setHorizontalGroup(
            credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(credentialsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(credentialsPanelLayout.createSequentialGroup()
                        .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(passwordField))
                    .addComponent(saveCredentialsCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(credentialsPanelLayout.createSequentialGroup()
                        .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usernameFormatLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        credentialsPanelLayout.setVerticalGroup(
            credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(credentialsPanelLayout.createSequentialGroup()
                .addComponent(saveCredentialsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usernameFormatLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(credentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(connectionDetailsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(serverUrlLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                    .addComponent(serverUrlTextField)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(previewLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(previewTextField))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(testConnectionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(credentialsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serverUrlLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(serverUrlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectionDetailsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(credentialsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(previewTextField)
                    .addComponent(previewLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(testConnectionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(testButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
        JButton testConnButton = (JButton) evt.getSource();
        testConnectionLabel.setText("Connecting...");
        testConnButton.setEnabled(false);
        if(controller.validate()) {
            String message = controller.testConnection();            
            if(message.equals("success")) {
                testConnectionLabel.setForeground(Color.GREEN);
                testConnectionLabel.setText("Success");
            } else {
                testConnectionLabel.setForeground(Color.RED);
                testConnectionLabel.setText(message);
            }            
        } else {
            
        }
        testConnButton.setEnabled(true);
    }//GEN-LAST:event_testButtonActionPerformed

    private void httpRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_httpRadioButtonActionPerformed
        controller.setProtocol(ConnectionSettings.PROTOCOL.HTTP);
        showPreview();
    }//GEN-LAST:event_httpRadioButtonActionPerformed

    private void httpsRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_httpsRadioButtonActionPerformed
        controller.setProtocol(ConnectionSettings.PROTOCOL.HTTPS);
        showPreview();
    }//GEN-LAST:event_httpsRadioButtonActionPerformed

    private void saveCredentialsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCredentialsCheckBoxActionPerformed
        JCheckBox cb = (JCheckBox) evt.getSource();        
        toggleCredentialsFields(cb.isSelected());
        controller.setSaveCredentials(cb.isSelected());
    }//GEN-LAST:event_saveCredentialsCheckBoxActionPerformed

    public void okButtonPressed() {
        try {
            controller.saveConnection();
        } catch (Exception ex) {
            //TODO: Use NotificationDisplayer and show a notification about the failure
            LOG.log(Level.INFO, ex.getLocalizedMessage());
        }
    }

    public void editOkButtonPressed() {
        try {
            controller.updateConnection(cs);
        } catch (Exception ex) {
            //TODO: Use NotificationDisplayer and show a notification about the failure
            LOG.log(Level.INFO, ex.getLocalizedMessage());
        }
    }
    
    public static AddTFSConnectionPanelController getController() {
        return controller;
    }
    
    public void toggleCredentialsFields(final boolean enabled) {
        passwordField.setEnabled(enabled);
        passwordLabel.setEnabled(enabled);
        usernameTextField.setEnabled(enabled);
        usernameLabel.setEnabled(enabled);
    }
    
    private void showPreview() {
        previewTextField.setText(controller.getUrlPreview());
    }
    
    private DocumentListener documentListener = new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateController(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateController(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateController(e);
        }
        
        private void updateController(DocumentEvent e) {
            controller.setName(nameTextField.getText());
            controller.setPath(pathTextField.getText());
            controller.setPortNumber(portTextField.getText());            
            controller.setProtocol(httpRadioButton.isSelected() ? ConnectionSettings.PROTOCOL.HTTP : ConnectionSettings.PROTOCOL.HTTPS);
            controller.setServerUrl(serverUrlTextField.getText());
            controller.setUsername(usernameTextField.getText());
            controller.setPassword(passwordField.getPassword());
            showPreview();
        }
    
    };

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel connectionDetailsPanel;
    private javax.swing.JPanel credentialsPanel;
    private javax.swing.JRadioButton httpRadioButton;
    private javax.swing.JRadioButton httpsRadioButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel pathLabel;
    private javax.swing.JTextField pathTextField;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portTextField;
    private javax.swing.JLabel previewLabel;
    private javax.swing.JTextField previewTextField;
    private javax.swing.ButtonGroup protocolButtonGroup;
    private javax.swing.JLabel protocolLabel;
    private javax.swing.JCheckBox saveCredentialsCheckBox;
    private javax.swing.JLabel serverUrlLabel;
    private javax.swing.JTextField serverUrlTextField;
    private javax.swing.JButton testButton;
    private javax.swing.JLabel testConnectionLabel;
    private javax.swing.JLabel usernameFormatLabel;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables
}
